package com.pengcheng.taskapplication.mvp.view.activity;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class ReSetPassword extends BaseActivity implements IView {

    @BindView(R.id.tv_center_title)
    TextView tv_title;

    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.confirm_pass)
    EditText confirm_pass;

    @BindView(R.id.tv_get_code)
    TextView btnCheckCode;

    @BindView(R.id.et_num)
    EditText et_num;

    @BindView(R.id.ll_next)
    LinearLayout ll_next;

    private boolean timeEnable = true;

    private String uid;
    private String newtoken;
    private UserPresenterV2 userPresenterV2;

    @Override
    protected int layoutResID() {
        return R.layout.activity_reset_pay_password;
    }

    @Override
    protected void initData() {
        tv_title.setText("重置密码");
        uid = SPUtils.getString(context, ConstantValue.SpType.userid);
        newtoken = SPUtils.getString(context, ConstantValue.SpType.newtoken);
        userPresenterV2=new UserPresenterV2(this);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.setPay)){
            ToastUtils.showShortToast(msg);
            ReSetPassword.this.finish();
        }
        if(actionType.equals(ConstantValue.RequestKey.sendCode)){
            timeEnable = false;
            btnCheckCode.setEnabled(false);
            CountDownTimer mCountDownTimer = new CountDownTimer(
                    60 * 1000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    long s = millisUntilFinished / 1000;
                    String text = s + "s";
                    btnCheckCode.setText(text);
                }

                @Override
                public void onFinish() {
                    timeEnable = true;
                    btnCheckCode.setText("发送验证码");
                    //boolean enable = !TextUtils.isEmpty(etPhone.getText().toString());
                    //btnCheckCode.setEnabled(enable);
                        btnCheckCode.setEnabled(true);
                    this.cancel();
                }
            };
            mCountDownTimer.start();
        }


    }

    @Override
    public void onFailed(String code, String msg) {

    }

    @OnClick({R.id.img_back,R.id.ll_next,R.id.tv_get_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.ll_next:
               String passstr= password.getText().toString();
                String confirmStr=confirm_pass.getText().toString();

                String code=  et_num.getText().toString();
                if(code==null||code.length()==0){
                    ToastUtils.showShortToast("请输入验证码");
                    return;
                }

                if(passstr==null||passstr.length()==0){
                    ToastUtils.showShortToast("请输入密码");
                    return;
                }
                if(!passstr.equals(confirmStr)){
                    ToastUtils.showShortToast("两次输入的密码不一致");
                    return;
                }

                //通过校验，调用修改密码
                setPayPassword(newtoken,uid,passstr,code);
                break;
            case R.id.tv_get_code:
                sendVcode(newtoken,uid);
                break;
        }
    }

    public void setPayPassword(String newtoken,String uid,String paypwd,String vcode){
        userPresenterV2.setPayPassword(newtoken ,uid,paypwd,vcode,ConstantValue.RequestKey.setPay);
    }



    public void sendVcode(String newtoken,String uid){
        userPresenterV2.sendVcode(newtoken,uid,ConstantValue.RequestKey.sendCode);
    }
}
